package com.mydemo.springlearning.functions.memoryshare.controller;

import com.mydemo.springlearning.functions.CRUDdemo.entity.User;
import com.mydemo.springlearning.functions.memoryshare.entity.BigUser;
import com.mydemo.springlearning.functions.memoryshare.service.BigUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author hd
 * @date 2022-07-14 14:59
 * @description
 */
@Slf4j
@RestController
@RequestMapping("bigUser")
public class ShareObjectController {

    private BigUserService bigUserService;

    @Autowired
    public void setBigUserService(BigUserService bigUserService) {
        this.bigUserService = bigUserService;
    }

    @RequestMapping(value = "encode")
    public String encodeTest() throws InterruptedException {
        return "中文测试字符编码";
    }

    /**
     * 暴力查所有耗时
     * @return 用户数量：500149，查询耗时: 14.131267
     */
    @RequestMapping(value = "amount")
    public String amount() {
        return bigUserService.getTotalUserAmount();
    }
    /**
     * 流式查询查所有耗时
     * @return 用户数量：500149，查询耗时: 14.131267
     */
    @RequestMapping(value = "amountStream")
    @Transactional
    public String amountByStream() {
        return bigUserService.getTotalUserAmountByStream();
    }/**
     * 流式查询查所有耗时
     * @return 用户数量：500149，查询耗时: 14.131267
     */
    @RequestMapping(value = "amountStreamThread")
    @Transactional
    public String amountByStreamMThread() {
        return bigUserService.getTotalUserAmountByStreamThread();
    }


    @RequestMapping("add")
    public String testAdd(String name) throws InterruptedException {
        User user = new User();
        user.setUserName(name);
        bigUserService.add(user);
        return "add ok";
    }

    @RequestMapping("get")
    public List<User> get() {
        List<User> users = bigUserService.get();
        return users;
    }

    @RequestMapping(value = "getAllBigUser")
    public String getAllBigUser() {
        List<BigUser> users = bigUserService.getAllBigUser();
        return "用户数量：" + users.size();
    }

    @RequestMapping("delete")
    public String delete(String name) {
        return bigUserService.delete(name);
    }


    @RequestMapping("getLimit/{limit}")
    public String getLimit(@PathVariable("limit") int limit) {
        return "用户数量" + bigUserService.getAllBigUserLimit(limit).size();
    }

    @RequestMapping("getLimitMulti/{limit}")
    public String getLimitMulti(@PathVariable("limit") int limit) {
        return "用户数量" + bigUserService.getAllBigUserByMultiThread(limit).size();
    }
}
